package com.souku.finance.application.db;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import com.souku.finance.application.R;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.UploadImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Android on 2017/1/11.
 */

public class AvatarUtil {

    public static void insert(Activity context, String mobile, String avaUrl) {
        if (null != avaUrl) {
            AvatarBeanDao dao = new AvatarBeanDao(context);
            if (dao.isContain(mobile) != -1) {
                dao.update(new AvatarBean(dao.isContain(mobile), mobile, avaUrl));
            } else {
                dao.add(new AvatarBean(mobile, avaUrl));
            }
        }
    }

    public static void ivHeadListenr(final Activity context, EditText editText, final ImageView imageView) {

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String mobile = s.toString().trim();
                if (MyTextUtil.isTelephone(mobile)) {
                    AvatarBeanDao dao = new AvatarBeanDao(context);
                    if (dao.isContain(mobile) != -1) {
                        ImageLoader.getInstance().displayImage(dao.getUrl(mobile), imageView, UploadImageUtil.getSimpleOptions(R.drawable.touxiang));
                    } else {
                        imageView.setImageResource(R.drawable.touxiang);
                    }
                } else {
                    imageView.setImageResource(R.drawable.touxiang);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
